<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
        }
        .bigBox {
            height: 500px;
            width: 500px;
            border: 5px solid salmon;
            position: relative;
            top: 30px;
            left: 50%;
            transform: translateX(-50%);
        }
        .smallBox {
            height: 50px;
            width: 50px;
            background-color: rgb(148, 237, 14);
            position: absolute;
            bottom: 0px;
        }
    </style>
</head>
<body>
    <div class="bigBox">
        <div class="smallBox"></div>
    </div>
    <button>开始</button>
    <button>暂停</button>

    <script>
        const bigBox = document.querySelector(".bigBox")
        const smallBox = document.querySelector(".smallBox")
        const btn = document.getElementsByTagName("button")

        let XS = bigBox.clientWidth - smallBox.clientWidth;  // 横向路程
        let YS = bigBox.clientHeight - smallBox.clientHeight;  // 纵向路程
        
        
        btn[0].onclick = function () {
            let begin = setInterval(function () {
                let x = smallBox.offsetLeft;  //小方块距离大方块的纵向距离
                let y = smallBox.offsetTop;  //小方块距离大方块的纵向距离
                console.log(x, y)
                if (x < XS && y == YS) {
                    smallBox.style.left = `${x + 10}px`
                }
                else if (y > 0 && y <= YS && x == XS) {
                    smallBox.style.top = `${y - 10}px`
                }
                else if (x > 0 && x <= XS && y == 0) {
                    smallBox.style.left = `${x - 10}px`
                }
                else if (y < YS && x == 0) {
                    smallBox.style.top = `${y + 10}px`
                }
                btn[1].onclick = function () {
                    clearInterval(begin)
                    console.log("暂停")
                }
            }, 100) 
        }
        
        
        console.log("XS:",XS)
        console.log("YS:",YS)
    </script>
</body>
</html>